<template>
  <div class="doctor-container">
    <!-- 顶部导航 -->
    <div class="header">
      <div class="header-content">
        <el-button @click="goBack" type="text" class="back-btn">
          <el-icon><ArrowLeft /></el-icon>
          返回
        </el-button>
        <h2>{{ departmentName }}</h2>
        <div></div>
      </div>
    </div>

    <!-- 搜索栏 -->
    <div class="search-section">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索医生姓名或专长"
        prefix-icon="Search"
        clearable
        @input="handleSearch"
      />
    </div>

    <!-- 医生的列表（网格卡片样式） -->
    <div class="doctor-list-grid">
      <div 
        v-for="doctor in filteredDoctors" 
        :key="doctor.id"
        class="doctor-card"
        @click="goToDoctorDetail(doctor.id)"
      >
        <div class="doctor-card-avatar">
          <img :src="doctor.avatar || '默认头像url'" alt="头像" />
        </div>
        <div class="doctor-card-info">
          <div class="doctor-card-name">{{ doctor.name }}</div>
          <div class="doctor-card-title">{{ doctor.title }}</div>
          <div class="doctor-card-specialty">{{ doctor.specialty }}</div>
        </div>
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading">
      <el-skeleton :rows="3" animated />
    </div>

    <!-- 空状态 -->
    <div v-if="!loading && filteredDoctors.length === 0" class="empty-state">
      <el-empty description="暂无相关医生" />
    </div>
  </div>
</template>

<script>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { mockAPI } from '../utils/mockData'

export default {
  name: 'DoctorList',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const doctors = ref([])
    const filteredDoctors = ref([])
    const searchKeyword = ref('')
    const loading = ref(false)
    const departmentName = ref('')

    const departmentId = route.params.departmentId

    // 获取医生列表
    const loadDoctors = async () => {
      loading.value = true
      try {
        const result = await mockAPI.getDoctors(departmentId)
        if (result.success) {
          doctors.value = result.data
          filteredDoctors.value = result.data
          
          // 获取科室名称
          const departmentsResult = await mockAPI.getDepartments()
          if (departmentsResult.success) {
            const department = departmentsResult.data.find(d => d.id === parseInt(departmentId))
            if (department) {
              departmentName.value = department.name
            }
          }
        }
      } catch (error) {
        ElMessage.error('获取医生列表失败')
        console.error('获取医生列表失败:', error)
      } finally {
        loading.value = false
      }
    }

    // 搜索功能
    const handleSearch = () => {
      if (!searchKeyword.value) {
        filteredDoctors.value = doctors.value
      } else {
        filteredDoctors.value = doctors.value.filter(doctor => 
          doctor.name.toLowerCase().includes(searchKeyword.value.toLowerCase()) ||
          doctor.specialty.toLowerCase().includes(searchKeyword.value.toLowerCase())
        )
      }
    }

    // 跳转到医生详情
    const goToDoctorDetail = (doctorId) => {
      router.push(`/doctor/${doctorId}`)
    }

    // 返回上一页
    const goBack = () => {
      router.go(-1)
    }

    onMounted(() => {
      loadDoctors()
    })

    return {
      doctors,
      filteredDoctors,
      searchKeyword,
      loading,
      departmentName,
      handleSearch,
      goToDoctorDetail,
      goBack
    }
  }
}
</script>

<style scoped>
.doctor-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 15px 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-btn {
  color: white;
  font-size: 16px;
  display: flex;
  align-items: center;
}

.back-btn .el-icon {
  margin-right: 5px;
}

.header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: bold;
}

.search-section {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.doctor-list-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 32px;
  max-width: 1200px;
  margin: 40px auto 0;
  padding: 0 20px 40px;
}

.doctor-card {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 16px 24px;
  cursor: pointer;
  transition: box-shadow 0.2s, transform 0.2s;
}

.doctor-card:hover {
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  transform: translateY(-4px) scale(1.03);
}

.doctor-card-avatar img {
  width: 110px;
  height: 110px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 18px;
  border: 3px solid #f0f0f0;
}

.doctor-card-info {
  text-align: center;
}

.doctor-card-name {
  font-size: 20px;
  font-weight: bold;
  color: #222;
  margin-bottom: 6px;
}

.doctor-card-title {
  font-size: 15px;
  color: #409EFF;
  margin-bottom: 4px;
  font-weight: 500;
}

.doctor-card-specialty {
  font-size: 13px;
  color: #888;
  margin-bottom: 0;
  min-height: 18px;
}

.loading {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.empty-state {
  max-width: 1200px;
  margin: 40px auto;
  padding: 0 20px;
  text-align: center;
}

@media (max-width: 768px) {
  .doctor-item {
    padding: 15px;
    flex-direction: column;
    text-align: center;
  }
  
  .doctor-avatar {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  .doctor-info {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  .doctor-schedule {
    margin-right: 0;
    margin-bottom: 15px;
  }
  
  .schedule-list {
    flex-direction: row;
    justify-content: center;
    gap: 8px;
  }
  
  .schedule-item {
    font-size: 10px;
    padding: 1px 4px;
  }
}
</style> 